﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;
using System.Web.Security;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Collections;
using System.IO;
using System.Drawing;
using System.Text;

public partial class goodsinfor : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["name"] == null)
        {
            Response.Redirect("~/error.aspx?");
        }

        if (Session["power"].ToString() == "查询用户")
        {
            Button1.Visible = false;
            Button2.Visible = false;
        }
        string JudgePower = Session["power"].ToString();
        if (JudgePower!= "超级管理员"&&JudgePower!="仓库管理员"&&JudgePower!="销售"&&JudgePower!="主管")
        {

            Response.Redirect("~/PowerError.aspx?");
        }
            if (!IsPostBack)
            {
                this.bind1();
            }
        
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/newgoods.aspx?");
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        if (GridView1.SelectedValue!=null)
        {
            SqlConnection myConn = GetConnection();
            myConn.Open();
            string sqlStr = "delete from [Goods] where GID='" + GridView1.SelectedValue.ToString() + "'";
            SqlCommand myCmd = new SqlCommand(sqlStr, myConn);
            myCmd.ExecuteNonQuery();
            myConn.Close();
            //Response.Write("<script>alert('删除成功!')</script>");
            Response.Redirect("~/goodsinfor.aspx?");
        }
        else
            Response.Write("<script>alert('未选择要删除的选项!')</script>");
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/daochu.aspx?");
    }
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        Label1.Visible = true;
        DropDownList2.Visible = true;
        Label2.Visible = true;
        TextBox1.Visible = true;
        Label3.Visible = true;
        TextBox2.Visible = true;
        Label4.Visible = true;
        TextBox3.Visible = true;
        Label5.Visible = true;
        TextBox4.Visible = true;
        Label6.Visible = true;
        TextBox5.Visible = true;
        Label7.Visible = true;
        TextBox6.Visible = true;
        Label8.Visible = true;
        TextBox7.Visible = true;
        Button4.Visible = true;

        string sql = "select GID,GCHName,GTYPEName,GXinhao,GBrand,GUnit,GProvider1,GProvider2,GDescribe,CName from Goods,Categories where Goods.GCategory=Categories.CID and  GID='" + GridView1.SelectedValue.ToString() + "' ";
        SqlConnection myConn = GetConnection();
        myConn.Open();
        SqlCommand com = new SqlCommand(sql, myConn);
        com.ExecuteNonQuery();
        SqlDataReader read = com.ExecuteReader();
        read.Read();
        TextBox1.Text = read["GCHName"].ToString().Trim();
        DropDownList2.SelectedValue = read["GTYPEName"].ToString().Trim();
        TextBox4.Text = read["GUnit"].ToString().Trim();
        TextBox5.Text = read["GDescribe"].ToString().Trim();
        TextBox6.Text = read["GProvider1"].ToString().Trim();
        TextBox7.Text = read["GProvider2"].ToString().Trim();
        TextBox2.Text = read["GXinhao"].ToString().Trim();
        TextBox3.Text = read["GBrand"].ToString().Trim();
        read.Close();
        myConn.Close();
    }

    public SqlConnection GetConnection()
    {
        string myStr = ConfigurationManager.AppSettings["ConnectionString"].ToString();
        SqlConnection myConn = new SqlConnection(myStr);
        return myConn;
    }

    protected void bind()
    {
        SqlConnection myConn = GetConnection();
        myConn.Open();
        string sqlStr = "select GID as 货品编号,GTYPEName as 类别,GCHName as 名称,GXinhao as 型号,GBrand as 品牌,GUnit as 单位,GProvider1 as 主供应商,GProvider2  as 次供应商,GDescribe as 备注 from [Goods] where GTYPEName ='" + DropDownList1.SelectedValue.ToString() + "'";
        SqlDataAdapter myDa = new SqlDataAdapter(sqlStr, myConn);
        DataSet myDs = new DataSet();
        myDa.Fill(myDs);
        GridView1.DataSource = myDs;
        GridView1.DataKeyNames = new string[] { "货品编号" };
        GridView1.DataBind();
        myDa.Dispose();
        myDs.Dispose();

        string sql1 = "select CName from Categories";
        SqlDataAdapter sqlDa = new SqlDataAdapter(sql1, myConn);
        DataSet sqlDs = new DataSet();
        sqlDa.Fill(sqlDs);
        DropDownList1.DataSource = sqlDs;
        DropDownList1.DataTextField = "CName";
        DropDownList1.DataBind();
        this.DropDownList1.Items.Insert(0, new ListItem("------------", "------------"));
        this.DropDownList1.Items.Insert(1, new ListItem("查看全部", "查看全部"));
        DropDownList2.DataSource = sqlDs;
        DropDownList2.DataTextField = "CName";
        DropDownList2.DataBind();
        sqlDa.Dispose();
        sqlDs.Dispose();

        myConn.Close();
    }

    protected void bind1()
    {
        SqlConnection myConn = GetConnection();
        myConn.Open();
        string sqlStr = "select GID as 货品编号,GTYPEName as 类别,GCHName as 名称,GXinhao as 型号,GBrand as 品牌,GUnit as 单位,GProvider1 as 主供应商,GProvider2  as 次供应商,GDescribe as 备注 from [Goods]";
        SqlDataAdapter myDa = new SqlDataAdapter(sqlStr, myConn);
        DataSet myDs = new DataSet();
        myDa.Fill(myDs);
        GridView1.DataSource = myDs;
        GridView1.DataKeyNames = new string[] { "货品编号" };
        GridView1.DataBind();
        myDa.Dispose();
        myDs.Dispose();


        string sql1 = "select CName from Categories";
        SqlDataAdapter sqlDa = new SqlDataAdapter(sql1, myConn);
        DataSet sqlDs = new DataSet();
        sqlDa.Fill(sqlDs);
        DropDownList1.DataSource = sqlDs;
        DropDownList1.DataTextField = "CName";
        DropDownList1.DataBind();
        this.DropDownList1.Items.Insert(0, new ListItem("------------", "------------"));
        this.DropDownList1.Items.Insert(1, new ListItem("查看全部", "查看全部"));
        DropDownList2.DataSource = sqlDs;
        DropDownList2.DataTextField = "CName";
        DropDownList2.DataBind();
        sqlDa.Dispose();
        sqlDs.Dispose();

        myConn.Close();
    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (DropDownList1.SelectedValue.ToString() == "查看全部")
        {
            this.bind1();
            //Response.Redirect("~/goodsinfor.aspx?");
        }
        else
        {
            this.bind();
            //Response.Redirect("~/goodsinfor.aspx?");
        }
    }
    protected void Button4_Click(object sender, EventArgs e)
    {
        if (TextBox1.Text.Trim() != "" && TextBox2.Text.Trim() != "")
        {
            SqlConnection myConn = GetConnection();
            myConn.Open();
            string sqll = "select GCHName from Goods where GCHName='" + TextBox1.Text.Trim() + "' and GTYPEName='" + DropDownList2.SelectedValue + "' and GXinhao='" + TextBox2.Text.Trim() + "' and GBrand='" + TextBox3.Text.Trim() + "'";
            SqlCommand cmd = new SqlCommand(sqll, myConn);
            cmd.ExecuteNonQuery();
            // SqlDataReader dr = new SqlDataReader();
            SqlDataReader dr = cmd.ExecuteReader();
            if (dr.Read())
            {
                dr.Close();
                Response.Write("<script>alert('已有该货品!')</script>");
            }
            else
           {
                dr.Close();
                string sql ="update Goods set GCHName='" + TextBox1.Text.Trim() + "',GTYPEName='" + DropDownList2.SelectedValue + "',GUnit='" + TextBox4.Text.Trim() + "',GDescribe='" + TextBox5.Text.Trim() + "',GProvider1='" + TextBox6.Text.Trim() + "',GProvider2='" + TextBox7.Text.Trim() + "',GXinhao='" + TextBox2.Text.Trim() + "',GBrand='" + TextBox3.Text.Trim() + "' where GID ='" + GridView1.SelectedValue.ToString() + "'";
                SqlCommand com = new SqlCommand(sql, myConn);
                com.ExecuteNonQuery();
                myConn.Close();
                Response.Write("<script>alert('修改成功!')</script>");
                Response.Redirect("~/success.aspx?");
            }
        }
        else
        {
            Response.Write("<script>alert('信息填写不完整!')</script>");
        }
    }
}